package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.TerminalUpgradePack;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.IdUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.constant.AppConstant;
import cc.yugu.yhdw.webserver.service.basic.TerminalUpgradePackService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/security/basic/terminal_upgrade_pack")
public class TerminalUpgradePackController extends SecurityController {

    final static String TERMINAL_UPGRADE_PATH = "/static/ykdg_upgrade/AGENT_ID/terminal.VERSION.apk";
    final static String UPGRADE_URL = "/security/basic_upload/terminal_upgrade.htm";

    @Autowired
    AppConfig config;

    @Autowired
    TerminalUpgradePackService terminalUpgradePackService;

    @RequestMapping("index.htm")
    public void index(String menuCode, Model model) throws Exception {
        model.addAttribute(MENU_CODE_NAME, menuCode);
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(TerminalUpgradePack search) {
        return PageResult.successResult(terminalUpgradePackService.findPage(search));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("edit.htm")
    public String edit(int id, Model model) {
        TerminalUpgradePack upgradePack = terminalUpgradePackService.find(id);
        if(upgradePack == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", upgradePack);
        return "/security/basic/terminal_upgrade_pack/edit";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add() {
    }

    @RequestMapping(value = "upload.htm", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file, Model model) throws Exception {
        File sourceFile = new File(getAppConfig().tempDir, IdUtils.uuid() + '.' + YhdwUtils.getFileSuffix(file.getOriginalFilename()));
        YhdwUtils.makeParentDir(sourceFile);
        file.transferTo(sourceFile);

        model.addAttribute("success", true);
        model.addAttribute("fileName", file.getOriginalFilename());
        model.addAttribute("filePath", AppConstant.PATH_TEMP + sourceFile.getName());
        return "/security/basic/terminal_upgrade_pack/upload";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping("create.htm")
    public ExtResult create(TerminalUpgradePack entity) throws IOException {
        Map<String, File> fileMap = new HashMap<String, File>();
        if(StringUtils.isNotEmpty(entity.getFilePath()) && entity.getFilePath().startsWith(AppConstant.PATH_TEMP)) {
            fileMap.put("file", new File(config.appDir, entity.getFilePath()));
        }else {
            return ExtResult.failResult("请上传");
        }
        terminalUpgradePackService.create(entity, fileMap);
        return ExtResult.successResult();
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping("update.htm")
    public ExtResult update(TerminalUpgradePack entity) throws IOException{
        entity.setUpdateTime(new Date());
        Map<String, File> fileMap = new HashMap<String, File>();
        if(StringUtils.isNotEmpty(entity.getFilePath()) && entity.getFilePath().startsWith(AppConstant.PATH_TEMP)) {
            fileMap.put("file", new File(config.appDir, entity.getFilePath()));
        }
        terminalUpgradePackService.update(entity, fileMap);
        return ExtResult.successResult();
}

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("view.htm")
    public String view(int id, Model model) {
        TerminalUpgradePack upgradePack = terminalUpgradePackService.find(id);
        if(upgradePack == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", upgradePack);
        return "/security/basic/terminal_upgrade_pack/view";
    }

    @RequestMapping(value = "download.htm")
    public void download(int id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        TerminalUpgradePack entity = terminalUpgradePackService.find(id);
        if(entity == null) {
            response.setStatus(404);
        } else {
            downloadSupport(getAppConfig().getFile(entity.getFilePath()), request, response, entity.getFileName());
        }
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping("delete.htm")
    public ExtResult delete(int id) throws IOException {
        return terminalUpgradePackService.delete(id);
    }
}
